package crud.server;

import java.util.ArrayList;
import java.util.List;

import crud.client.data.Catalogue;
import crud.client.data.Colonne;
import crud.client.data.ColonneToOne;
import crud.client.data.DataView;
import crud.client.data.LigneDonnee;
import crud.client.data.Table;
import crud.client.data.authentication.AuthenticatedUserAdapter;
import crud.client.data.authentication.User;
import crud.client.service.CRUDService;
import crud.dao.DAOLocator;
import crud.server.util.ServiceLocator;



public class CRUDServiceImpl implements CRUDService {


    public List<Colonne> getColonneList(Table table)
    {
    	try
		{
    		return DAOLocator.getColonneDAO().loadAllByTable(table);
			
		} catch (Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
    }

	@Override
	public List<LigneDonnee> getDonneeLineList(Table table, List<Colonne> colonneList)
	{
	   	try
		{
			return DAOLocator.getDonneeDAO().loadLigneDonneeList(table, colonneList);
		} catch (Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<Table> getTableList()
	{
	   	try
		{
			return DAOLocator.getTableDAO().loadAll();
		} catch (Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}


	@Override
	public List<DataView> getDataList(ColonneToOne colonneToOne)
	{
	  	try
		{
    		return DAOLocator.getDonneeDAO().loadAll(colonneToOne);
			
		} catch (Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public void updateData(Table table, List<LigneDonnee> donneeListToDelete, List<LigneDonnee> donneeListToSave)
	{
	   	try
		{
    		DAOLocator.getDonneeDAO().updateData(table, donneeListToDelete, donneeListToSave);
			
		} catch (Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public boolean isDataUsed(Table table, LigneDonnee ligneDonneeToCheck)
	{
	   	try
		{
    		return DAOLocator.getDonneeDAO().isDataUsed(table, ligneDonneeToCheck);
		} catch (Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<DataView> getTableCatalogue(String codeTable)
	{
		List<Catalogue> catalogueList = DAOLocator.getCatalogueDAO().loadTable(codeTable);
		List<DataView> dataviewList = new ArrayList<DataView>();
		
		for (Catalogue catalogue : catalogueList)
		{
			DataView dataView = new DataView();
			dataView.setKey(catalogue.getCodeDetail());
			dataView.setLabel(catalogue.getLibelle());
			dataView.setData(catalogue.getCodeDetail());
			dataviewList.add(dataView);
		}
		
		return dataviewList;
	}

	@Override
	public DataView getLigneCatalogue(String codeTable, String codeDetail)
	{
		Catalogue catalogue = DAOLocator.getCatalogueDAO().get(codeTable, codeDetail);
		DataView dataView = new DataView();
		dataView.setKey(codeDetail);
		dataView.setData(codeDetail);
		if (catalogue != null)
		{
			dataView.setLabel(catalogue.getCodeDetail());
		}
		return dataView;
	}

	@Override
	public String getLibelleCatalogue(String codeTable, String codeDetail)
	{
		Catalogue catalogue = DAOLocator.getCatalogueDAO().get(codeTable, codeDetail);
		
		if (catalogue == null)
		{
			return null;
		}
		
		return catalogue.getLibelle();
	}

	@Override
	public List<DataView> getEmptyDataViewListCallback()
	{
		return new ArrayList<DataView>();
	}

	@Override
	public User authenticate(String login, String password)
	{
	   	try
		{
			return ServiceLocator.getAuthenticationAdapter().getUser(login, password);
		} catch (Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public Boolean authenticationRequired()
	{
	   	try
		{
			if (ServiceLocator.getAuthenticationAdapter() != null)
		{
			return true;
		}
		else
		{
			return false;
		}
		} catch (Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public User getAuthenticatedUser()
	{
	   	try
		{
	   		AuthenticatedUserAdapter authenticatedUserAdapter =   ServiceLocator.getAuthenticatedUserAdapter();
	   		if (authenticatedUserAdapter == null)
	   		{
	   			return null;
	   		}
	   		
	   		return authenticatedUserAdapter.getAuthenticatedUser();
		} catch (Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	
}